package com.quanyan.stadium.component.mq;

import com.alibaba.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import com.alibaba.rocketmq.common.message.MessageExt;
import com.quanyan.common.constants.Constants;
import com.quanyan.mq.enums.MqConsumerStatus;
import com.quanyan.mq.mq.consumer.AbstractMqConsumer;
import com.quanyan.mq.mq.consumer.MqTopic;
import com.quanyan.orderpay.service.OrderPayService;
import com.quanyan.stadium.constants.GameConstants;
import com.quanyan.stadium.service.gameSign.GameSignUpService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.List;

import javax.annotation.PostConstruct;

/**
 * Created with IntelliJ IDEA.
 * User: zyq
 * Date: 2016/7/6 0006
 * Time: 17:04
 */

@Component(value = "gamePayMq")
public class GamePayMq extends AbstractMqConsumer {

    private static Logger logger = LoggerFactory.getLogger(GamePayMq.class);

    @Value("${mq.nameServer.address}")
    private String nameServerAddress;

    @Autowired
    private GameSignUpService gameSignUpService;

    @Override
    protected MqConsumerStatus process(List<MessageExt> messages, ConsumeConcurrentlyContext context) {

        MessageExt messageExt = messages.get(0);
        //处理订单系统支付成功消息
        if(OrderPayService.TOPIC_ORDER_STATUS_NOTIFY.equals(messageExt.getTopic()) && Constants.STADIUM_SYSTEM_CODE.equals(messageExt.getTags())){
            //支付成功
            //gameSignUpService.orderPayOperate(messageExt);
        }
        return MqConsumerStatus.CONSUMER_SUCCESS;
    }

    @PostConstruct
    void run() {
        super.start();
    }

    @Override
    protected String getConsumerGroup() {
        return GameConstants.STADIUM_GROUP_ORDER_PAY;
    }

    @Override
    protected String getNameServerAddress() {
        return nameServerAddress;
    }

    @Override
    protected String getInstanceName() {
        return GameConstants.STADIUM_INSTANCE_ORDER_PAY;
    }

    @Override
    protected MqTopic[] getMqTopics() {
        MqTopic[] mqTopics = new MqTopic[1];
        mqTopics[0] = new MqTopic(OrderPayService.TOPIC_ORDER_STATUS_NOTIFY, Constants.STADIUM_SYSTEM_CODE);
        return mqTopics;
    }
}
